/*
//	CP2013 Traffic Simulator Assignment :: (MVC) Model :: Intersection Class (Function Definitions)
//	Copyright (c) 2010, Adam Rehn
//
//	This file contains the function definitions for this component.
*/
#include "intersection.h"

//Constructor
intersection::intersection(rectangle area, unsigned int greenLength)
{
	//Read the rectangle that we cover
	this->area = area;
	
	//Read the length of a green light (in cycles)
	this->greenLength = greenLength;
	
	/*
	//Create the list of possible states
	intersectionState NS_GREEN  = {GREEN  ,RED};
	intersectionState NS_YELLOW = {YELLOW ,RED};
	intersectionState WE_GREEN  = {RED    ,GREEN};
	intersectionState WE_YELLOW = {RED    ,YELLOW};
	
	//Populate the vector (green lights are applied greenLength times)
	for (int i = 0; i < greenLength; ++i)
		statesCycle.push_back(NS_GREEN);
	
	statesCycle.push_back(NS_YELLOW);
	
	for (int i = 0; i < greenLength; ++i)
		statesCycle.push_back(WE_GREEN);
	
	statesCycle.push_back(WE_YELLOW);
	
	//Set our state to the first one
	setState(0);
	*/
}

//This will return true if one of the lights is yellow
bool intersection::yellowOn() const
{
	return (lightNS.state == YELLOW || lightWE.state == YELLOW);
}

//Use this to cycle the intersection lights to the next state
void intersection::cycleState()
{
	//Move to the next state in the cycle
	setState(cycleIndex + 1);
}

//Helper functions
void intersection::setState(unsigned int n)
{
	//For numbers larger than the greatest index, roll over
	n = n % statesCycle.size();
	
	//Update the traffic lights
	lightNS.state = statesCycle[n].lightNS;
	lightWE.state = statesCycle[n].lightWE;
	
	//Update our state details and index
	state = statesCycle[n];
	cycleIndex = n;
}
